<?php
namespace app\home\controller;
/*
 *  用户中心
 * */
use app\common\controller\Home;
use think\facade\Cache;
use think\facade\Db;
use think\facade\View;

class User extends Home
{
    public function initialize()
    {
//        $redis = new \think\cache\driver\Redis(['select' => 1]);
        parent::initialize();
    }

    /*
     *  登陆注册
     *  return user_info
     *  */
    public function login(){
        if($this->request->isAjax()){
            $address = $this->request->post('address','');
            if(empty($address)){
                return $this->failResponse('注册失败[钱包地址不能为空]');
            }
            $redis = new \think\cache\driver\Redis(['select' => 1]);
            $redis_key = md5("user_login_{$address}");
            if($redis->get($redis_key)){
                return $this->failResponse('刷新太快了哟，页面正在努力加载中...');
            }
            $redis->set($redis_key,1,120);
            try{
                $user = new \app\common\model\User();
                if($user_info = $user->userInfoByAddress($address)){
                    $redis->delete($redis_key);
                    return $this->successResponse('success', $address);
                }else{
                    if(!$p_share_code = self::getPShareCode()){
                        $redis->delete($redis_key);
                        return $this->failResponse('注册失败[p_share_code]');
                    }
                    $parent_info = $user->userInfoByShareCode($p_share_code);
                }
            }catch (\Exception $e){
                $redis->delete($redis_key);
                return $this->failResponse('请求失败[err]');
            }
        }else{
            return $this->failResponse('fail');
        }
    }

    public function logPage(){
        $address = $this->request->get("address");
        View::assign('address', $address);
        return View::fetch("log_page");
    }

    /*
     *  记录
     * */
    public function loglist(){
        if($this->request->isAjax()){
            $post_data = $this->request->post();
            $page = $post_data['page'] ?? 1;
            $address = $post_data['address'] ?? '';
            $limit = 15;
            if(!empty($address)){
                $userModel = new \app\common\model\User();
                $uid = $userModel->where("heco_address",$address)->value("id");
            }
            if(empty($uid)){
                return $this->apiResponse(0,'fail');
            }
            $where = [];
            $where[] = ['uid','=',$uid];
            $cashListModel = new \app\common\model\CashList();
            $list = $cashListModel->where($where)->page($page,$limit)->order("id desc")->field("trade_hash,token_money,status,c_time")->select()->toArray();
            foreach ($list as $k => $v){
                $list[$k]['c_time'] = date("Y-m-d H:i:s",$v['c_time']);
            }
            $is_bottom = 0;
            if(count($list) < $limit){
                $is_bottom = 1;
            }
            $return_data = [
                'list' => $list,
                'page' => $page,
                'is_bottom' => $is_bottom
            ];
            return $this->apiResponse(1,'success',$return_data);
        }
        return $this->apiResponse(0,'fail');
    }

}